﻿@model KL.Store.Web.Models.GroupMenuModel
@using KL.Store.Core
@{
    ViewBag.Title = "Stok Filtreleme";
    Layout = "~/Views/Shared/_LayoutReportPanel.cshtml";
}
<div id="processResult"></div>
<div class="row">
    <br />
    <form class="form-horizontal" id="StokFiltreForm">
        <div class="col-xs-12">
            <div class="form-group">
                <div class="col-xs-4">
                    <select name="SelectedCategory" id="@(ViewBag.Id)" class="form-control categorySelect" style = "width:100%;">
                        <option value="0">Kategori Seçilmedi</option>
                        @foreach (var c in Model.GroupListA)
                        {
                            <option value="@(c.CategoryId)" @{if (c.CategoryId == Model.SelectedGroup)
                                                              {<text> selected="selected"</text>}}>@(c.CategoryName) (@(c.CategoryType)) @(c.CategoryId > 0 ? " - " + c.CategoryId.ToString() : "")@(!string.IsNullOrEmpty(c.ParentCategoryName) ? " (" + c.ParentCategoryName + ")" : "")</option>
                        }
                    </select>
                </div>
                <div class="col-xs-4">
                    <select name="SelectedBrand" id="@(ViewBag.Id)" class="form-control categorySelect" style = "width:100%;">
                        <option value="0">Marka Seçilmedi</option>
                        @foreach (var c in Model.GroupListM)
                        {
                            <option value="@(c.CategoryId)" @{if (c.CategoryId == Model.SelectedGroup)
                                                              {<text> selected="selected"</text>}}>@(c.CategoryName) (@(c.CategoryType)) @(c.CategoryId > 0 ? " - " + c.CategoryId.ToString() : "")@(!string.IsNullOrEmpty(c.ParentCategoryName) ? " (" + c.ParentCategoryName + ")" : "")</option>
                        }
                    </select>
                </div>
                <div class="col-xs-4">
                    <select name="SelectedPage" id="@(ViewBag.Id)" class="form-control categorySelect" style = "width:100%;">
                        <option value="0">Sayfa Seçilmedi</option>
                        @foreach (var c in Model.GroupListS)
                        {
                            <option value="@(c.CategoryId)" @{if (c.CategoryId == Model.SelectedGroup)
                                                              {<text> selected="selected"</text>}}>@(c.CategoryName) (@(c.CategoryType)) @(c.CategoryId > 0 ? " - " + c.CategoryId.ToString() : "")@(!string.IsNullOrEmpty(c.ParentCategoryName) ? " (" + c.ParentCategoryName + ")" : "")</option>
                        }
                    </select>
                </div>
            </div>
            <div class="form-group">
                <div class="col-xs-4 col-sm-3 col-md-2">
                    <label for="FreeCargo">Ücretsiz Kargo</label>
                    <select name="FreeCargo" id="FreeCargo" class="form-control">
                        <option value="T">Tümü</option>
                        <option value="E">Evet</option>
                        <option value="H">Hayır</option>
                    </select>
                </div>
                <div class="col-xs-4 col-sm-3 col-md-2">
                    <label for="FastCargo">Hızlı Kargo</label>
                    <select name="FastCargo" id="FastCargo" class="form-control">
                        <option value="T">Tümü</option>
                        <option value="E">Evet</option>
                        <option value="H">Hayır</option>
                    </select>
                </div>
                <div class="col-xs-4 col-sm-3 col-md-2">
                    <label for="NewProduct">Yeni Ürün</label>
                    <select name="NewProduct" id="NewProduct" class="form-control">
                        <option value="T">Tümü</option>
                        <option value="E">Evet</option>
                        <option value="H">Hayır</option>
                    </select>
                </div>
                <div class="col-xs-4 col-sm-3 col-md-2">
                    <label for="OpportunityProduct">Fırsat Ürünü</label>
                    <select name="OpportunityProduct" id="OpportunityProduct" class="form-control">
                        <option value="T">Tümü</option>
                        <option value="E">Evet</option>
                        <option value="H">Hayır</option>
                    </select>
                </div>
                <div class="col-xs-4 col-sm-3 col-md-2">
                    <label for="CampaignProduct">Kampanyalı Ürün</label>
                    <select name="CampaignProduct" id="CampaignProduct" class="form-control">
                        <option value="T">Tümü</option>
                        <option value="E">Evet</option>
                        <option value="H">Hayır</option>
                    </select>
                </div>
                <div class="col-xs-4 col-sm-3 col-md-2">
                    <label for="ShowAsCampaignProduct">Kampanyalı Göster</label>
                    <select name="ShowAsCampaignProduct" id="ShowAsCampaignProduct" class="form-control">
                        <option value="T">Tümü</option>
                        <option value="E">Evet</option>
                        <option value="H">Hayır</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <div class="col-xs-4 col-sm-3 col-md-2">
                    <label for="CheckStock">Stok Tut</label>
                    <select name="CheckStock" id="CheckStock" class="form-control">
                        <option value="T">Tümü</option>
                        <option value="E">Evet</option>
                        <option value="H">Hayır</option>
                    </select>
                </div>
                <div class="col-xs-4 col-sm-3 col-md-2">
                    <label for="GiftString">Hediye Metni</label>
                    <select name="GiftString" id="GiftString" class="form-control">
                        <option value="T">Tümü</option>
                        <option value="D">Dolu</option>
                        <option value="B">Boş</option>
                    </select>
                </div>
                <div class="col-xs-4 col-sm-3 col-md-2">
                    <label for="SpecialText">Özel Metin</label>
                    <select name="SpecialText" id="SpecialText" class="form-control">
                        <option value="T">Tümü</option>
                        <option value="D">Dolu</option>
                        <option value="B">Boş</option>
                    </select>
                </div>
                <div class="col-xs-4 col-sm-3 col-md-2">
                    <label style="display: block;">&nbsp;</label>
                    <button type="button" class="btn btn-primary" onclick="MainFilter(1, 1);">Filtrele</button>
                </div>
            </div>
            <div id="StokFiltreFormExpander">
                <i class="glyphicon glyphicon-chevron-up"></i>
            </div>
        </div>
        <div class="clearfix"></div>
    </form>
    <div id="StokFiltreListesi">
    </div>
</div>
<script>
    var expanded = true;
    $('#StokFiltreFormExpander').click(function ()
    {
        var h = $('#StokFiltreForm').height() + 25;
        if (expanded)
        {
            $('#StokFiltreForm').animate({ 'margin-top': '-' + h + 'px' });
            $(this).find('.glyphicon').removeClass('glyphicon-chevron-up');
            $(this).find('.glyphicon').addClass('glyphicon-chevron-down');
            expanded = false;
        }
        else
        {
            $('#StokFiltreForm').animate({ 'margin-top': '0px' });
            $(this).find('.glyphicon').removeClass('glyphicon-chevron-down');
            $(this).find('.glyphicon').addClass('glyphicon-chevron-up');
            expanded = true;
        }
    });
    function MainFilter(order, sort)
    {
        var vSelectedCategory = $('select[name=SelectedCategory]').val();
        var vSelectedBrand = $('select[name=SelectedBrand]').val();
        var vSelectedPage = $('select[name=SelectedPage]').val();
        var vFreeCargo = $('select[name=FreeCargo]').val();
        var vFastCargo = $('select[name=FastCargo]').val();
        var vNewProduct = $('select[name=NewProduct]').val();
        var vOpportunityProduct = $('select[name=OpportunityProduct]').val();
        var vCampaignProduct = $('select[name=CampaignProduct]').val();
        var vShowAsCampaignProduct = $('select[name=ShowAsCampaignProduct]').val();
        var vGiftString = $('select[name=GiftString]').val();
        var vSpecialText = $('select[name=SpecialText]').val();
        var vCheckStock = $('select[name=CheckStock]').val();
        Filtrele(order, sort, vSelectedCategory, vSelectedBrand, vSelectedPage, vFreeCargo, vFastCargo, vNewProduct, vOpportunityProduct, vCampaignProduct, vShowAsCampaignProduct, vGiftString, vSpecialText, vCheckStock);
    }


    function DetailFilter(order, sort)
    {
        var vSelectedCategory = $('#StokFiltreListesi input[name=vSelectedCategory]').val();
        var vSelectedBrand = $('select[name=SelectedBrand]').val();
        var vSelectedPage = $('select[name=SelectedPage]').val();
        var vFreeCargo = $('#StokFiltreListesi input[name=vFreeCargo]').val();
        var vFastCargo = $('#StokFiltreListesi input[name=vFastCargo]').val();
        var vNewProduct = $('#StokFiltreListesi input[name=vNewProduct]').val();
        var vOpportunityProduct = $('#StokFiltreListesi input[name=vOpportunityProduct]').val();
        var vCampaignProduct = $('#StokFiltreListesi input[name=vCampaignProduct]').val();
        var vShowAsCampaignProduct = $('#StokFiltreListesi input[name=vShowAsCampaignProduct]').val();
        var vGiftString = $('#StokFiltreListesi input[name=vGiftString]').val();
        var vSpecialText = $('#StokFiltreListesi input[name=vSpecialText]').val();
        var vCheckStock = $('#StokFiltreListesi input[name=vCheckStock]').val();
        Filtrele(order, sort, vSelectedCategory, vSelectedBrand, vSelectedPage, vFreeCargo, vFastCargo, vNewProduct, vOpportunityProduct, vCampaignProduct, vShowAsCampaignProduct, vGiftString, vSpecialText, vCheckStock);
    }

    function Filtrele(vorder, vsort, vSelectedCategory, vSelectedBrand, vSelectedPage, vFreeCargo, vFastCargo, vNewProduct, vOpportunityProduct, vCampaignProduct, vShowAsCampaignProduct, vGiftString, vSpecialText, vCheckStock)
    {
        loadingMsg();
        $('#StokFiltreListesi').load('/ReportPanel/StokFiltreGet', {
            SelectedCategory: vSelectedCategory,
            SelectedBrand: vSelectedBrand,
            SelectedPage: vSelectedPage,
            FreeCargo: vFreeCargo,
            FastCargo: vFastCargo,
            NewProduct: vNewProduct,
            OpportunityProduct: vOpportunityProduct,
            CampaignProduct: vCampaignProduct,
            ShowAsCampaignProduct: vShowAsCampaignProduct,
            GiftString: vGiftString,
            SpecialText: vSpecialText,
            CheckStock: vCheckStock,
            order: vorder,
            sort: vsort
        }, function ()
        {
            $('.loadingMsg').remove();
        });
    }
    $('#SelectAll').live('change', function ()
    {
        if ($(this).is(':checked'))
        {
            $('.SelectedProduct').attr('checked', 'checked');
            $(this).attr('title', 'Seçimleri Kaldır');
        }
        else
        {
            $('.SelectedProduct').removeAttr('checked');
            $(this).attr('title', 'Tümünü Seç');
        }
    });
    $('#UygulaIslemSec').live('change', function ()
    {
        var sec = $(this);
        var value = sec.val();
        if (value == '')
        {
            $('.UygulaIslemTF').hide(0);
            $('.UygulaIslemEH').hide(0);
            $('.UygulaIslemText').hide(0);
            $('.UygulaIslemPost').hide(0);

            $('#UygulaIslemTF').val('');
            $('#UygulaIslemEH').val('');
            $('#UygulaIslemText').val('');
            $('#UygulaIslemPost').attr('disabled', 'disabled');
        }
        else if (value == 'UCRETSIZKARGO' || value == 'HIZLIKARGO' || value == 'YENIURUN' || value == 'FIRSATURUN' || value == 'KAMPANYALIURUN' || value == 'KAMPANYALIGOSTER')
        {
            $('.UygulaIslemTF').show(0);
            $('.UygulaIslemEH').hide(0);
            $('.UygulaIslemText').hide(0);
            $('.UygulaIslemPost').show(0);

            $('#UygulaIslemTF').val('');
            $('#UygulaIslemEH').val('');
            $('#UygulaIslemText').val('');
            $('#UygulaIslemPost').attr('disabled', 'disabled');
        }
        else if (value == 'STOKTUT')
        {
            $('.UygulaIslemTF').hide(0);
            $('.UygulaIslemEH').show(0);
            $('.UygulaIslemText').hide(0);
            $('.UygulaIslemPost').show(0);

            $('#UygulaIslemTF').val('');
            $('#UygulaIslemEH').val('');
            $('#UygulaIslemText').val('');
            $('#UygulaIslemPost').attr('disabled', 'disabled');
        }
        else if (value == 'GIFTSTRING' || value == 'OZELMETIN')
        {
            $('.UygulaIslemTF').hide(0);
            $('.UygulaIslemEH').hide(0);
            $('.UygulaIslemText').show(0);
            $('.UygulaIslemPost').show(0);

            $('#UygulaIslemTF').val('');
            $('#UygulaIslemEH').val('');
            $('#UygulaIslemText').val('');
            $('#UygulaIslemPost').removeAttr('disabled');
        }
    });
    $('#UygulaIslemTF').live('change', function ()
    {
        if ($(this).val() == '')
        {
            $('#UygulaIslemPost').attr('disabled', 'disabled');
        }
        else
        {
            $('#UygulaIslemPost').removeAttr('disabled');
        }
    });
    $('#UygulaIslemEH').live('change', function ()
    {
        if ($(this).val() == '')
        {
            $('#UygulaIslemPost').attr('disabled', 'disabled');
        }
        else
        {
            $('#UygulaIslemPost').removeAttr('disabled');
        }
    });
    function IslemUygula()
    {
        var checkedCount = $('.SelectedProduct:checked').length;
        var completed = 0;
        var notcompleted = 0;
        var secim = $('#UygulaIslemSec').val();
        var changeThis = '';
        var changedValue = '';
        var changedType = '';
        switch (secim)
        {
            case 'UCRETSIZKARGO': changeThis = 'FreeCargoField'; changedValue = $('#UygulaIslemTF').val(); changedType = 'TF'; break;
            case 'HIZLIKARGO': changeThis = 'FastCargoField'; changedValue = $('#UygulaIslemTF').val(); changedType = 'TF'; break;
            case 'YENIURUN': changeThis = 'NewProductField'; changedValue = $('#UygulaIslemTF').val(); changedType = 'TF'; break;
            case 'FIRSATURUN': changeThis = 'OpportunityProductField'; changedValue = $('#UygulaIslemTF').val(); changedType = 'TF'; break;
            case 'KAMPANYALIURUN': changeThis = 'CampaignProductField'; changedValue = $('#UygulaIslemTF').val(); changedType = 'TF'; break;
            case 'KAMPANYALIGOSTER': changeThis = 'ShowAsCampaignProductField'; changedValue = $('#UygulaIslemTF').val(); changedType = 'TF'; break;
            case 'STOKTUT': changeThis = 'CheckStockField'; changedValue = $('#UygulaIslemEH').val(); changedType = 'EH'; break;
            case 'GIFTSTRING': changeThis = 'GiftStringField'; changedValue = $('#UygulaIslemText').val(); changedType = 'Text'; break;
            case 'OZELMETIN': changeThis = 'SpecialTextField'; changedValue = $('#UygulaIslemText').val(); changedType = 'Text'; break;
            default: changeThis = ''; changedValue = ''; changedType = ''; break;
        }
        if (checkedCount > 0)
        {
            if (secim != '' && confirm('Tüm seçili kayıtlar güncellenecek, emin misiniz?'))
            {
                loadingMsg();
                $('.SelectedProduct:checked').each(function ()
                {
                    var id = $(this).data('id');
                    $.post('/ReportPanel/StokFiltreSet', {
                        ProductId: id,
                        Field: secim,
                        ValueTF: $('#UygulaIslemTF').val(),
                        ValueEH: $('#UygulaIslemEH').val(),
                        ValueText: $('#UygulaIslemText').val()
                    }, function (result)
                    {
                        //successed
                        if (result == 'fail')
                        {
                            notcompleted++;
                        }
                        else
                        {
                            completed++;
                            if (changedType == 'TF')
                            {
                                if (changedValue == '1')
                                {
                                    $('#Product' + id).find('.' + changeThis).html('<b>Evet</b>');
                                }
                                else
                                {
                                    $('#Product' + id).find('.' + changeThis).html('Hayır');
                                }
                            }
                            if (changedType == 'EH')
                            {
                                if (changedValue == 'E')
                                {
                                    $('#Product' + id).find('.' + changeThis).html('<b>Evet</b>');
                                }
                                else
                                {
                                    $('#Product' + id).find('.' + changeThis).html('Hayır');
                                }
                            }
                            if (changedType == 'Text')
                            {
                                if (changedValue != '')
                                {
                                    $('#Product' + id).find('.' + changeThis).html('<b>Dolu</b>');
                                }
                                else
                                {
                                    $('#Product' + id).find('.' + changeThis).html('Boş');
                                }
                            }
                        }

                        if (completed == checkedCount)
                        {
                            //DetailFilter($('input[name=vOrder]').val(), $('input[name=vSort]').val());
                            $('#processResult').show(0).html('<div class="bg-info"><p><i class="fa fa-info-circle"></i>&nbsp;Bilgiler Kaydedildi</p></div>').delay(5000).hide(0);
                        }
                        else if (completed + notcompleted == checkedCount)
                        {
                            hataMesaji('İşlem tamamlandı ancak bazı stokların güncellenmesi sırasında hata oluştu. Yeniden filtreleyerek kontrol ediniz.');
                        }
                    });

                    //alert('aqlattın :( -- ' + id);
                });
            }
        }
        else
        {
            hataMesaji('Hiç ürün seçilmemiş.');
        }
    }
</script>
